﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace AliIotAmqpWorker
{
    /// <summary>
    /// 将枚举类型转换为结构
    /// </summary>
    public class EnumHelper
    {
        /// <summary>
        /// 从枚举类型和它的特性读出并返回队列
        /// </summary>
        /// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        /// <returns>键值对</returns>
        public static List<EnumStruct> ConvertEnumToList(Type enumType,Object model)
        {
            List<EnumStruct> list = new List<EnumStruct>();
            Type typeDescription = typeof(DescriptionAttribute);
            FieldInfo[] fields = enumType.GetFields();
            var propertyInfos= enumType.GetRuntimeProperties();
            string strText = string.Empty;
            int Value = 0;

            //foreach (FieldInfo field in fields)
            //{
            //    if (field.FieldType.IsEnum)
            //    {
            //        Value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
            //        object[] arr = field.GetCustomAttributes(typeDescription, true);
            //        if (arr.Length > 0)
            //        {
            //            DescriptionAttribute Description = (DescriptionAttribute)arr[0];
            //            strText = Description.Description;
            //        }
            //        else
            //        {
            //            strText = field.Name;
            //        }
            //        EnumStruct eStruct = new EnumStruct();
            //        eStruct.Description = strText;
            //        eStruct.Key = field.Name;
            //        eStruct.Value = Value;
            //        list.Add(eStruct);
            //    }
            //}

            foreach (var item in enumType.GetRuntimeProperties())
            {
                var type = item.PropertyType.Name;
                var IsGenericType = item.PropertyType.IsGenericType;
                var list2 = item.PropertyType.GetInterface("IEnumerable", false);
                Console.WriteLine($"属性名称：{item.Name}，类型：{type}，值：{item.GetValue(model)}");
                if (IsGenericType && list != null)
                {
                    var listVal = item.GetValue(model) as IEnumerable<object>;
                    if (listVal == null) continue;
                    foreach (var aa in listVal)
                    {
                        var dtype = aa.GetType();
                        foreach (var bb in dtype.GetProperties())
                        {
                            var dtlName = bb.Name.ToLower();
                            var dtlType = bb.PropertyType.Name;
                            var oldValue = bb.GetValue(aa);
                            if (dtlType == typeof(decimal).Name)
                            {
                                int dit = 4;
                                if (dtlName.Contains("price") || dtlName.Contains("amount"))
                                    dit = 2;
                                bb.SetValue(aa, Math.Round(Convert.ToDecimal(oldValue), dit, MidpointRounding.AwayFromZero));
                            }
                            Console.WriteLine($"子级属性名称：{dtlName}，类型：{dtlType}，值：{oldValue}");
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 根据描述名称获取该描述所对应的键值
        /// </summary>
        /// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        /// <param name="Description">描述名称</param>
        /// <returns>键值</returns>
        public static int GetEnumValueFromDescript(Type enumType, string Description)
        {
            NameValueCollection nvc = new NameValueCollection();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            int KeyValue;

            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    KeyValue = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute DescriptionStr = (DescriptionAttribute)arr[0];
                        if (DescriptionStr.Description.Equals(Description))
                        {
                            return KeyValue;
                        }
                    }
                }
            }
            return 0;
        }
    }
    /// <summary>
    /// 枚举类型结构化
    /// </summary>
    public class EnumStruct
    {
        public string Key { set; get; }
        public string Description { set; get; }
        public object Value { set; get; }
    }

    
}
